/*
 * led.c
 *
 * Created: 14/12/2013 19:37:50
 *  Author: usuario
 */ 

#include "led.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


static void prvBlinkyTask		( void *pvParameters );

void LED_setup( void )
{
	gpio_configure_pin( BLINKY_LED,  ( PIO_OUTPUT_1 | PIO_DEFAULT ) );
	gpio_configure_pin( ERROR_LED ,  ( PIO_OUTPUT_1 | PIO_DEFAULT ) );

	xTaskCreate( prvBlinkyTask, "Blinky",  BLINKY_TASK_STACK_SIZE, NULL, BLINKY_TASK_PRIORITY, NULL);
}

void LED_set( unsigned portBASE_TYPE led, signed portBASE_TYPE xValue )
{
	if (led == ERROR_LED) xValue=!xValue;
	
	if (xValue)
	{
		taskENTER_CRITICAL();
			gpio_set_pin_high( led );
		taskEXIT_CRITICAL();
	} else
	{
		taskENTER_CRITICAL();
		gpio_set_pin_low( led );
		taskEXIT_CRITICAL();
	}
}

void LED_toggle( unsigned portBASE_TYPE led )
{
	taskENTER_CRITICAL();
		gpio_toggle_pin( led );
	taskEXIT_CRITICAL();
}

static void prvBlinkyTask( void *pvParameters )
{
	portTickType xNextWakeTime;
	
	xNextWakeTime = xTaskGetTickCount();
	
	for( ;; )
	{
		vTaskDelayUntil( &xNextWakeTime, BLINKY_ELAPSE );
		
		if (check_assert()) LED_set( ERROR_LED , pdTRUE);
				
		LED_toggle( BLINKY_LED );
	}
}